package com.test.controller;

import com.test.bean.Result;
import com.test.bean.User;
import com.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("user")
public class DispatcherController {

    @Autowired
    private UserService userService;

    //1.用Ajar完成登录操作，但不跳转页面
    @ResponseBody
    @RequestMapping(value="login",method = {RequestMethod.GET})
    public Object doAjaxLogin(User user, HttpSession session, HttpServletResponse response){

        //创建一个AjaxResult对象，用于保存各种
        Result result = new Result();
        User dbUser = userService.queryForLogin(user);

        int code;//表示验证结果
        //登录成功
        if(dbUser != null) {
            //将用户放入session中
            //session.setAttribute("loginUser",dbUser.getUsername());

            //获取是否记住用户名单选框的状态
            if("true".equals(user.getFlag())){
                // 完成记住用户名的功能:
                Cookie cookie = new Cookie("username",user.getUsername());
                //设置有效路径:
                cookie.setPath("/ssmtest");
                // 设置有效时间:
                cookie.setMaxAge(60*60*24);// 保存24小时
                // 将cookie回写到浏览器：
                response.addCookie(cookie);}


            result.setCode(200);
            result.setMessage("密码正确");

        }
        else{
            result.setCode(400);
            result.setMessage("密码或用户名错误");
        }
        return result;
    }

    //2.用Ajar完成注册操作，但不跳转页面
    @ResponseBody
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Object doAjaxRegister(User user){

        //创建一个Result对象
        Result result = new Result();

        User dbUser = userService.queryForReg(user.getUsername());

        if(dbUser != null){
            //该用户已经被注册
            result.setMessage("导入失败");
            result.setCode(400);
        }else{
            try {
                userService.insertUser(user);
                result.setMessage("导入成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("导入失败");
                result.setCode(400);//异常
            }
        }
        return result;
    }


    //3.修改密码  // @RequestParam表示从表单传过来的参数，值要与ajar表单的data保持一致
    @ResponseBody
    @RequestMapping(value = "change",method = RequestMethod.PUT)
    public Result handle_change_password( User user, HttpSession session){
        Result result = new Result();
//        System.out.println("开始执行修改密码逻辑..."+old_password+","+new_password);
        User dbUser = userService.queryForReg(user.getUsername());
        if(dbUser != null) {
            userService.changePassword(user);
            //调用服务层来提供改密服务
            //        System.out.println("密码修改成功！");
            result.setMessage("密码修改成功");
            result.setCode(200);

        }else{
            result.setMessage("密码修改失败");
            result.setCode(400);
        }
        return result;
    }

}
